"""
Case Type   : python驱动psycopg2
Case Name   : 安装驱动
Create At   : 2023/8/23
Owner       : @ningyali
Description :
    1.解压PDBC得到lib,替换psycopg2
    2.输入python3,输入import psycopg2
Expect      :
    1.执行成功
    2.执行成功
History     :
"""
import os
import subprocess
import unittest

from testcase.CONNECTORS.PDBC.PSYCOPG2. \
    Opengauss_Function_Connect_Psycopg_Common import Psycopg2Common
from testcase.utils.CommonSH import CommonSH
from testcase.utils.Logger import Logger
from testcase.utils.Constant import Constant


class Psycopg1(unittest.TestCase):
    def setUp(self):
        self.log = Logger()
        self.constant = Constant()
        text = f'-----{os.path.basename(__file__)} start-----'
        self.log.info(text)
        self.pri_sh = CommonSH('PrimaryDbDmode')
        self.conn_info = {'dbname': self.pri_sh.node.db_name,
                          'user': self.pri_sh.node.db_user,
                          'password': self.pri_sh.node.db_password,
                          'host': self.pri_sh.node.ssh_host,
                          'port': self.pri_sh.node.db_port}
        self.psycopg_com = Psycopg2Common(**self.conn_info)

    def test_1(self):
        text = '----step1: 解压PDBC得到lib,替换psycopg2 expect: 执行成功----'
        res = self.psycopg_com.decompress_pkg()
        self.assertTrue(res, f'执行失败: {text}')
        res = self.psycopg_com.set_new_psycopg2()
        self.assertTrue(res, f'执行失败: {text}')

        text = '----step3: 配置lib expect: 成功----'
        self.log.info(text)
        res = self.psycopg_com.set_lib_path()
        self.assertTrue(res, f'执行失败: {text}')

        text = '----step4: 输入python3,输入import psycopg2 expect: 执行成功----'
        self.log.info(text)
        cmd = f'python3 -c "import psycopg2"'
        self.log.info(cmd)
        res = subprocess.getoutput(cmd)
        self.assertNotIn("No module named 'psycopg2'", res, f'执行失败: {text}')

        text = '----step5: 建表;expect: 成功----'
        self.log.info(text)
        sql = 'drop TABLE if exists t_fetch0016;' \
            'CREATE TABLE t_fetch0016 (ID INT PRIMARY KEY,Value NVARCHAR(50));' \
            'INSERT INTO t_fetch0016 VALUES (1, \'A\'), (2, \'B\'), (3, \'C\');'
        f_sql = self.psycopg_com.format_sql(sql)
        run_sql = self.psycopg_com.join_run_sql(f_sql)
        res = self.psycopg_com.run_sql(sql=run_sql)
        self.assertNotIn('error', res.lower(), f'执行失败: {text}')

        text = '----step6: 定义游标,并使用FETCH NEXT遍历结果集; expect: 成功----'
        self.log.info(text)
        sql = 'begin;' \
            'DECLARE c_fetch0016 CURSOR FOR SELECT ID, Value FROM t_fetch0016 ORDER BY ID;' \
            'FETCH NEXT FROM c_fetch0016;' \
            'select @@fetch_status;'
        f_sql = self.psycopg_com.format_sql(sql)
        run_sql = self.psycopg_com.join_run_sql(f_sql)
        res = self.psycopg_com.run_sql(sql=run_sql)
        expect = "[(0,)]"
        self.assertIn(expect, res, '执行失败:' + text)

        text = '----step7: 清理环境; expect: 成功----'
        self.log.info(text)
        sql = 'drop table if exists t_fetch0016;'
        f_sql = self.psycopg_com.format_sql(sql)
        run_sql = self.psycopg_com.join_run_sql(f_sql)
        res = self.psycopg_com.run_sql(sql=run_sql)
        self.log.info(res)
        self.assertNotIn('ERROR', res, f'执行失败: {text}')

    def tearDown(self):
        text = '----run teardown----'
        self.log.info(text)

        text1 = f'----后置操作：删除解压文件目录----'
        self.log.info(text1)
        res1 = self.psycopg_com.clean_decompress_file()
        self.assertTrue(res1, f'执行失败: {text1}')

        text = f'-----{os.path.basename(__file__)} end-----'
        self.log.info(text)
